package com.eCommerce.server.dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.CriteriaImpl;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.eCommerce.corecontract.criteria.ComposantCriteria;

import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.server.corebackend.dao.AbstractCommonHibernateDAO;
import com.eCommerce.server.corebackend.dao.IComposantDAO;

public class ComposantDAOImpl extends AbstractCommonHibernateDAO implements
		IComposantDAO {
	
	
	public ComposantDAOImpl()
	{
		super();
	}
	private static Logger log =Logger.getLogger(ComposantDAOImpl.class.getName());
	public void delete(ComposantDTO composantDTO)
			throws EcommerceException {
		
		try {
			Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
			   String hql = "delete from ComposantDTO where id = :id";
		        Query query = session.createQuery(hql);
		        query.setLong("id",composantDTO.getId());
		        int rowCount = query.executeUpdate();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
	}

	@SuppressWarnings("unchecked")
	public List find(ComposantCriteria composantCriteria)
			throws EcommerceException {
		// TODO Auto-generated method stub
		
		try {
			 if (!TransactionSynchronizationManager.isSynchronizationActive())
				   TransactionSynchronizationManager.initSynchronization();
			Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			ComposantDTO composantDTO=new ComposantDTO();
			composantDTO.setCode(composantCriteria.getCode());
			composantDTO.setLibelle(composantCriteria.getLibelle());//(composantCriteria.getCode());
			composantDTO.setId(composantCriteria.getId());
			Example exampleComposant= Example.create(composantDTO).ignoreCase().enableLike(MatchMode.ANYWHERE);
			
			List listOfComposant=null;
			listOfComposant=session.createCriteria(ComposantDTO.class)			
			.add(exampleComposant)
			
			.list();
		
			return listOfComposant;
		
		} 
		catch (Exception e) {
			Object[] arg=new Object[1];
			
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
		
	}

	@SuppressWarnings("unchecked")
	public List findAll() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(ComposantDTO.class);
			
			List listOfComposantDTOReturn=this.getHibernateTemplate().findByCriteria(criteria);
	
			return listOfComposantDTOReturn;
			
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
	}

	public ComposantDTO findOne(ComposantDTO composantDTO)
			throws EcommerceException {
		
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(ComposantDTO.class);
			criteria.add(Restrictions.eq("id", composantDTO.getId()));
			criteria.setFetchMode("categories", FetchMode.JOIN);
			List listOfComposant=getHibernateTemplate().findByCriteria(criteria);
			composantDTO = (ComposantDTO) listOfComposant.get(0);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			arg[0] = composantDTO.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-003",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}

		return composantDTO;
	}

	public Long insert(ComposantDTO composantDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().save(composantDTO);
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			arg[0] = composantDTO.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-001",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		return composantDTO.getId();
	}

	public void update(ComposantDTO composantDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().update(composantDTO);
			getHibernateTemplate().getSessionFactory().getCurrentSession().flush(); 
			getHibernateTemplate().getSessionFactory().getCurrentSession().clear();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			arg[0] = composantDTO.getLibelle();
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOCOMPOSANT-002",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}

	}
	public List findComposantByIdCategorie(CategorieDTO categorieDTO)throws EcommerceException
	{
		 if (!TransactionSynchronizationManager.isSynchronizationActive())
			   TransactionSynchronizationManager.initSynchronization();
		Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
		List resulList =session.createQuery("from ComposantDTO c where c.categorieDTO=:idCategorie")
		.setEntity("idCategorie", categorieDTO)
		.list();
		return resulList;
		
	}

}
